package com.ljm.sys.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.common.enums.DataStatus;
import com.ljm.common.enums.ResultStatus;
import com.ljm.common.excuption.Assert;
import com.ljm.common.page.PageDto;
import com.ljm.common.util.BeanUtils;
import com.ljm.common.util.StringUtil;
import com.ljm.sys.auth.api.SysAuthPermissionApi;
import com.ljm.sys.auth.dto.SysAuthPermissionDto;
import com.ljm.sys.auth.entity.SysAuthPermission;
import com.ljm.sys.auth.mapper.SysAuthPermissionMapper;
import com.ljm.sys.auth.service.ISysAuthPermissionService;
import com.ljm.sys.org.dto.SysOrgUserDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统权限 服务实现类
 * </p>
 *
 * @author HuamingChen
 * @since 2018-12-19
 */
@Service
@Transactional
public class SysAuthPermissionServiceImpl extends ServiceImpl<SysAuthPermissionMapper, SysAuthPermission>
        implements ISysAuthPermissionService, SysAuthPermissionApi {
    @Resource
    private SysAuthPermissionMapper sysPermissionMapper;

    @Override
    public Set<String> getUserPermission(Long userId)  {
        List<String> roles = sysPermissionMapper.getUserPermission(userId);
        if (StringUtil.isEmpty(roles)) {
            return Collections.emptySet();
        } else {
            return new HashSet<>(roles);
        }
    }

    @Override
    public void saveData(SysOrgUserDto userDto, SysAuthPermissionDto dto)  {
        SysAuthPermission po=new SysAuthPermission();
        BeanUtils.copyProperties(dto,po);
        if(StringUtil.isEmpty(dto.getId())){
            po.setCreateTime(LocalDateTime.now());
            po.setCreater(userDto.getLoginName());
            po.setStatus(DataStatus.NORMAL.getCode());
            po.setOrgId(userDto.getOrgId());
        }
        po.setEditor(userDto.getLoginName());
        po.setEditTime(LocalDateTime.now());
        this.saveOrUpdate(po);
    }

    @Override
    public void del(List<Long> ids)  {
        if(StringUtil.isEmpty(ids)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键集合为空");
        }
        this.removeByIds(ids);
    }

    @Override
    public SysAuthPermissionDto getDataById(Long id)  {
        if(StringUtil.isEmpty(id)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        SysAuthPermissionDto dto=new SysAuthPermissionDto();
        SysAuthPermission po=this.getById(id);
        if(!StringUtil.isEmpty(po)){
            BeanUtils.copyProperties(po,dto);
        }
        return dto;
    }

    @Override
    public PageDto<SysAuthPermissionDto> getDataByPage(String permissionName, String permissionCode,
                                                       String groupName, Integer pageNo, Integer pageSize)  {
        PageDto<SysAuthPermissionDto> pageDto=new PageDto<>(pageNo,pageSize);
        QueryWrapper<SysAuthPermission> queryWrapper=new QueryWrapper<>();
        IPage<SysAuthPermission> page=new Page<>(pageNo,pageSize);
        if(!StringUtil.isEmpty(permissionName)){
            queryWrapper.like("permission_name",permissionName);
        }
        if(!StringUtil.isEmpty(permissionCode)){
            queryWrapper.like("permission_code",permissionCode);
        }
        if(!StringUtil.isEmpty(groupName)){
            queryWrapper.like("group_name",groupName);
        }
        //queryWrapper.orderByDesc("group_name");
        //queryWrapper.orderByAsc("sort");
        queryWrapper.orderByDesc("edit_time");
        page=this.page(page,queryWrapper);
        if(!StringUtil.isEmpty(page.getRecords())){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(BeanUtils.copyPropertiesByList(page.getRecords(),SysAuthPermissionDto.class));
        }
        return pageDto;
    }

    @Override
    public Map<String, List<SysAuthPermissionDto>> getPermissionsWithGroup()  {
        Map<String, List<SysAuthPermissionDto>> map=new HashMap<>();
        QueryWrapper<SysAuthPermission> wrapper=new QueryWrapper<>();
        List<SysAuthPermission> pos=this.list(wrapper);
        if(!StringUtil.isEmpty(pos)){
            List<SysAuthPermissionDto> dtos= BeanUtils.copyPropertiesByList(pos,SysAuthPermissionDto.class);
            //分组
           map=dtos.stream().sorted(Comparator.comparing(SysAuthPermissionDto::getSort)).collect(Collectors.groupingBy(SysAuthPermissionDto::getGroupName));
        }
        return map;
    }

    @Override
    public List<SysAuthPermissionDto> getAllPermission()  {
        List<SysAuthPermissionDto> list=new ArrayList<>();
        QueryWrapper<SysAuthPermission> wrapper=new QueryWrapper<>();
        List<SysAuthPermission> pos= this.list(wrapper);
        if(!StringUtil.isEmpty(pos)){
            list= BeanUtils.copyPropertiesByList(pos,SysAuthPermissionDto.class);
        }
        return list;
    }
}
